package net.muchine.test.mongodb;

import java.net.UnknownHostException;
import java.util.regex.Pattern;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;

public class SimpleClient {
	private MongoClient client;
	private DB db;
	
	public SimpleClient() {
		try {
			this.connect();	
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void connect() throws Exception {
		// connect to localhost:27017
		this.client = new MongoClient();
		this.db = client.getDB("test");
	}
	
	public void testRegex() {
		DBCollection collection = this.db.getCollection("test");
		
		Pattern abc = Pattern.compile("abc");
		BasicDBObject query = new BasicDBObject("name", new BasicDBObject("$not", abc));
		
//		query = new BasicDBObject("name", "abde");
		
		DBCursor cursor = collection.find(query);
		try {
			while(cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			cursor.close();
		}
	}
	
	public static void main(String[] args) {
		SimpleClient client = new SimpleClient();
		client.testRegex();
	}
}
